Skip to content

Add configurable realm support for token authentication - #163

Merged
fschmtt merged 3 commits into
fschmtt:mainfrom
phillipfickl:make-auth-realm-configurable
Nov 2, 2025
Merged

Add configurable realm support for token authentication#163
fschmtt merged 3 commits into
fschmtt:mainfrom
phillipfickl:make-auth-realm-configurable

Conversation

@phillipfickl

Copy link
Copy Markdown
Contributor

Background
The token fetching was hardcoded to hit /realms/master/protocol/openid-connect/token, which is a real pain for anyone not using the master realm. In enterprise setups, you typically have realm admins who can only access their specific realm - they don't get master realm privileges.

What this fixes

  • Realm admins can now authenticate against their own realm instead of being forced to use master
  • Supports the common enterprise pattern where different teams/environments have their own realms
  • Removes a major blocker for organisations that follow least-privilege security practices

Implementation
Added a configurable realm parameter that defaults to "master" so existing code keeps working exactly as before. No breaking changes.

// Still works the same way
$keycloak = new Keycloak($baseUrl, $username, $password);

// Now you can also do this
$keycloak = new Keycloak($baseUrl, $username, $password, realm: 'my-realm');

I decided against changing signatures of other methods to keep this PR as non-invasive as possible. I'd leave these major API changes to you, @fschmtt

@phillipfickl
phillipfickl marked this pull request as draft September 15, 2025 10:44
@phillipfickl

Copy link
Copy Markdown
Contributor Author

PHPstan is complaining. I'll fix and reopen the PR afterwards

@codecov

codecov Bot commented Sep 15, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.14%. Comparing base (a8d8687) to head (9c9e799).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #163   +/-   ##
=======================================
  Coverage   94.14%   94.14%           
=======================================
  Files          27       27           
  Lines         905      905           
=======================================
  Hits          852      852           
  Misses         53       53           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@phillipfickl
phillipfickl marked this pull request as ready for review September 15, 2025 11:23
@phillipfickl phillipfickl changed the title Allow authenticating against a different realm then master Allow authenticating against a different realm than master Sep 15, 2025
@phillipfickl phillipfickl changed the title Allow authenticating against a different realm than master Add configurable realm support for token authentication Sep 15, 2025
@phillipfickl

Copy link
Copy Markdown
Contributor Author

Hey @fschmtt - any chance you'd consider merging this? If there's a problem with the PR please let me know.
Thank you!

@fschmtt

fschmtt commented Nov 2, 2025

Copy link
Copy Markdown
Owner

Hey @phillipfickl thank you for your patience!

I planned to refactor the authentication but I honestly didn't find the time for it (admittedly also not the motivation 😅).

Your changes LGTM, I'll merge them and make a new release.

Again, sorry for not responding earlier!

@fschmtt
fschmtt merged commit c76c1c5 into fschmtt:main Nov 2, 2025
13 checks passed
@phillipfickl
phillipfickl deleted the make-auth-realm-configurable branch November 6, 2025 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants